package com.peoplesay;

import android.R.color;
import android.app.Activity;
import android.app.AlertDialog;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import java.util.HashMap;
import java.util.Random;
import com.google.ads.*;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class PeopleSayActivity extends PeopleSayAbstractActivity {

	private OnLongClickListener myLongClickListener;
	private String[] urlLinks = { 	"http://www.youtube.com/watch?v=m31TOu27kzk&ob=av3e",
			"http://www.youtube.com/watch?v=47aS3uSCHLc",
			"http://www.youtube.com/watch?v=fXDpfhehb6I&ob=av3e",
			"http://www.youtube.com/watch?v=LpaDBD84ET0",
			"http://www.youtube.com/watch?v=o5MJbZ4l4J8&ob=av3e",
			"http://www.youtube.com/watch?v=6HP2escR3qQ&ob=av3e",
			"http://www.youtube.com/watch?v=yRvJylbSg7o",
			"http://www.youtube.com/watch?v=f-x8t0JOnVw",
			 };

	@Override
	protected void initalizeView() {
		setContentView(R.layout.main);
		mainLayoutView = (TableLayout) findViewById(R.id.mainTableLayout);
		mainLayoutView.setBackgroundDrawable(myResource
				.getDrawable(R.drawable.singlegirl));

	}

	@Override
	protected void start() {
	}

	@Override
	protected void initalizeListeners() {
		myClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int viewId = v.getId();
				playClip(viewId);
				mainLayoutView.setBackgroundDrawable(myResource
						.getDrawable(categoryDrawable[(Integer) buttonMap
								.get(viewId)]));
			}

		};

		myLongClickListener = new OnLongClickListener() {

			public boolean onLongClick(View v) {

				int viewId = v.getId();
				String url = urlLinks[(Integer) buttonMap.get(viewId)];
				Intent browerIntent = new Intent(Intent.ACTION_VIEW,
						Uri.parse(url));
				startActivity(browerIntent);

				return true;
			}

		};

		buttonMap = new HashMap<Integer, Integer>();
		Drawable buttonPic = myResource.getDrawable(R.drawable.button_shape);

		for (int i = 0; i < categoryCount; i++) {
			Button b = (Button) findViewById(buttons[i]);
			b.setOnClickListener(myClickListener);
			b.setOnLongClickListener(myLongClickListener);
			buttonMap.put(buttons[i], i);

			b.getBackground().setAlpha(100);

		}

	}

	private void playClip(int view) {

		int viewId = (Integer) buttonMap.get(view);

		int soundFile = soundFiles[viewId][rnd.nextInt(categoryFiles[viewId])];
		if (soundFile != 0) {

			if (player != null) {
				try {
					player.stop();
					player.release();
				} catch (IllegalStateException e) {

				} finally {
					player = null;
				}
			}

			player = MediaPlayer.create(this, soundFile);
			player.setLooping(false);
			player.start();
			player.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer player) {
					player.release();
					player = null;
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_soundboard, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.ms_switch:
			startActivity(new Intent(this, PeopleSayGameActivity.class)
					.putExtra("EXTRA_GAME_SETTING", gameSetting));
			return true;
		case R.id.ms_setting:
			openGameSettingDialog();
			return true;

		}
		return false;
	}
	
	protected void openGameSettingDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.gameSettingTitle)
				.setItems(R.array.difficulty,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
								gameSetting = i;
							}
						}).show();
	}


}